-
Notifications
You must be signed in to change notification settings - Fork 0
/
BST - Minimum & Maximum value.cpp
73 lines (65 loc) · 1.63 KB
/
BST - Minimum & Maximum value.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
using namespace std;
struct NODE {
int key;
NODE *left;
NODE *right;
};
NODE *createNode(int key){
NODE *node = new NODE;
node->key = key;
return node;
}
NODE *insert(NODE *root, int key){
if(!root) return createNode(key);
if(key < root->key){
root->left = insert(root->left, key);
} else if(key > root->key) { // NOTE: use else conditon only to keep duplicate values
root->right = insert(root->right, key);
}
return root;
}
int minValue(NODE *root){
while(root->left != NULL){
root = root->left;
}
return root->key;
}
int maxValue(NODE *root){
while(root->right != NULL){
root = root->right;
}
return root->key;
}
void print(NODE *root, string indent = "", bool right = true){
if (root != nullptr) {
cout << indent;
if(right){
cout << (indent.length() == 0 ? "Root╌" : "R╌╌╌╌");
indent += " ";
} else {
cout << "L╌╌╌╌";
indent += "┆ ";
}
cout << "[" << root->key << "]" << endl;
print(root->left, indent, false);
print(root->right, indent, true);
}
}
int main(){
NODE *root = NULL;
root = insert(root, 5);
insert(root, 1);
insert(root, 3);
insert(root, 6);
insert(root, 6);
insert(root, 4);
insert(root, 2);
insert(root, 7);
cout << "Print the tree ↴ " << endl;
cout << "-----------------" << endl;
print(root);
cout << endl;
cout << "Minimum value: " << minValue(root) << endl;
cout << "Maximum value: " << maxValue(root) << endl;
}